/*
 * Copyright (C) 2008 Alessandro Sivieri <alessandro.sivieri@chimera-bellerofonte.eu>.
 *
 *  This file is part of Daex.
 *
 *  Daex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.chimera_bellerofonte.tesi.ui.models;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.logging.Level;

import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

import eu.chimera_bellerofonte.tesi.DaexLogger;
import eu.chimera_bellerofonte.tesi.datatypes.Song;


/**
 * Classe di gestione dell'import e dell'export di elementi
 * di tipo Song per l'implementazione della copia/drag and drop;
 * la sottoclasse implementa nello specifico come utilizzare i
 * componenti per gestire i dati in entrata ed in uscita.
 * 
 * @see eu.chimera_bellerofonte.tesi.datatypes.Song
 * @author Sivieri Alessandro
 * @author Vitucci Nicola
 * @version 12/mag/07
 */
public abstract class SongTransferHandler extends TransferHandler
{
	static final long serialVersionUID = 1L;

	protected abstract void setSongs(JComponent c, Song[] s);
	protected abstract SongTransferable getTransferable(JComponent c);

	/**
	 * Trasferimento autorizzato solo se il flavor e' supportato ed
	 * il componente ricevente e' una JList (dato che in questo
	 * applicativo si vuole implementare il drop solo nelle liste di
	 * encoding).
	 * 
	 * @see javax.swing.JList
	 * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent, java.awt.datatransfer.DataFlavor[])
	 */
	@Override
	public boolean canImport(JComponent arg0, DataFlavor[] arg1)
	{
		for(DataFlavor f: arg1)
		{
			if(f.equals(SongTransferable.getDefaultFlavor()) && arg0 instanceof JList)
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * @see javax.swing.TransferHandler#createTransferable(javax.swing.JComponent)
	 */
	@Override
	protected Transferable createTransferable(JComponent arg0)
	{
		return getTransferable(arg0);
	}

	/**
	 * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
	 */
	@Override
	public int getSourceActions(JComponent arg0)
	{
		return TransferHandler.COPY;
	}

	/**
	 * @see javax.swing.TransferHandler#importData(javax.swing.JComponent, java.awt.datatransfer.Transferable)
	 */
	@Override
	public boolean importData(JComponent arg0, Transferable arg1)
	{
		if(canImport(arg0, arg1.getTransferDataFlavors()))
		{
			try
			{
				Song[] s=(Song[]) arg1.getTransferData(SongTransferable.getDefaultFlavor());
				setSongs(arg0, s);
				return true;
			}
			catch (Exception e)
			{
				DaexLogger.getDaexLogger().log(Level.SEVERE, "Impossibile ricavare l'oggetto Song dal sistema di trasferimento!", e);
			}
		}
		return false;
	}
	
}
